/**
 * FileName: MySearchView
 * Author: Administrator
 * Date: 2020/11/30 0030 10:24
 * Description:
 */
package sz.szsmk.citizencard.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import sz.szsmk.citizencard.R;

/**
 * @ClassName: MySearchView
 * @Description: java类作用描述
 * @Author: Administrator
 * @Date: 2020/11/30 0030 10:24
 */
public class MySearchView extends LinearLayout implements View.OnClickListener {

    private final TypedArray ta;
    private EditText etInput;
    private LinearLayout etLayout;
    private ImageView ivDelete;
    private Button btnSearch;
    private Context mContext;
    private SearchViewListener mListener;
    private String searchType;//定义搜索框类型  round 圆形 rec 圆角矩形

    /**
     * 设置搜索回调接口
     *
     * @param listener 监听者
     */
    public void setSearchViewListener(SearchViewListener listener) {
        mListener = listener;
    }

    public void setSearchType(String type) {
        searchType = type;
    }

    public MySearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        LayoutInflater.from(context).inflate(R.layout.searchview_layout, this);
        ta = context.obtainStyledAttributes(attrs, R.styleable.MySearchView);
        searchType = ta.getString(R.styleable.MySearchView_searchType);
        initViews();
    }

    private void initViews() {

        etInput = (EditText) findViewById(R.id.search_et);
        etInput.setHint(ta.getString(R.styleable.MySearchView_hintText));//输入框提示
        etInput.setHintTextColor(ta.getColor(R.styleable.MySearchView_hintColor, mContext.getResources().getColor(R.color.color_99a3a8)));//提示语的颜色
        etInput.setTextColor(ta.getColor(R.styleable.MySearchView_textColor, mContext.getResources().getColor(R.color.color_99a3a8)));//提示语的颜色
        etLayout = (LinearLayout) findViewById(R.id.et_layout);
        ivDelete = (ImageView) findViewById(R.id.search_delete);
        btnSearch = (Button) findViewById(R.id.search_btn);
//        btnSearch.setTextColor(ta.getColor(R.styleable.MySearchView_textColor, mContext.getResources().getColor(R.color.white)));//搜索按钮的颜色
//        btnSearch.setText(ta.getString(R.styleable.MySearchView_btnText));//搜索按钮的文字
        ivDelete.setOnClickListener(this);
        btnSearch.setOnClickListener(this);
        if ("round".equals(searchType)) {
            etLayout.setBackground(mContext.getResources().getDrawable(R.drawable.shape_bg_search_round));
            btnSearch.setBackground(mContext.getResources().getDrawable(R.drawable.shape_bg_search_text_unfocus));
        } else if ("rec".equals(searchType)) {
            btnSearch.setBackground(mContext.getResources().getDrawable(R.drawable.shape_bg_search_text_unfocus_rec));
            etLayout.setBackground(mContext.getResources().getDrawable(R.drawable.shape_bg_search_rec));
        }

        etInput.addTextChangedListener(new EditChangedListener());
        etInput.setOnClickListener(this);
        etInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    notifyStartSearching(etInput.getText().toString());
                }
                return true;
            }
        });
    }

    /**
     * 通知监听者 进行搜索操作
     *
     * @param text
     */
    private void notifyStartSearching(String text) {
        if (mListener != null) {
            mListener.onSearch(etInput.getText().toString());
        }
        //隐藏软键盘
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }


    private class EditChangedListener implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            if (!"".equals(charSequence.toString())) {
                ivDelete.setVisibility(VISIBLE);
                if ("round".equals(searchType)) {
                    btnSearch.setBackground(mContext.getResources().getDrawable(R.drawable.shape_bg_search_text));
                } else if ("rec".equals(searchType)) {
                    btnSearch.setBackground(mContext.getResources().getDrawable(R.drawable.shape_bg_search_text_rec));
                }
                //更新autoComplete数据
            } else {
                ivDelete.setVisibility(GONE);
                if ("round".equals(searchType)) {
                    btnSearch.setBackground(mContext.getResources().getDrawable(R.drawable.shape_bg_search_text_unfocus));
                } else if ("rec".equals(searchType)) {
                    btnSearch.setBackground(mContext.getResources().getDrawable(R.drawable.shape_bg_search_text_unfocus_rec));
                }
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.search_btn) {
            notifyStartSearching(etInput.getText().toString());
        } else if (id == R.id.search_delete) {
            etInput.setText("");
        }
    }

    /**
     * search view回调方法
     */
    public interface SearchViewListener {


        /**
         * 开始搜索
         *
         * @param text 传入输入框的文本
         */
        void onSearch(String text);

//        /**
//         * 提示列表项点击时回调方法 (提示/自动补全)
//         */
//        void onTipsItemClick(String text);
    }

}